﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "UiCore.h"
#include "MouseCursor.h"
#include "../base/EventMouse.h"
#include "../ui/DefultWidget.h"
#include "../render/Camera.h"
#include "../node/Scene.h"
#include "../base/Debuger.h"
#include "../base/CharacterEvent.h"
#include "../base/EventKey.h"
#include "../base/WidgetEvent.h"

namespace SkyEngine2d
{
	
	UiCore::~UiCore()
	{
	
	}
	
	bool UiCore::onKeyPressEvent(EventKey* event)
	{
		if (m_inputCaptureWidget && m_inputCaptureWidget != m_rootWidget)
		{
			return m_inputCaptureWidget->onEvent(event);
		}
		return false;
	}
	
	bool UiCore::onKeyReleaseEvent(EventKey* event)
	{
		if (m_inputCaptureWidget && m_inputCaptureWidget != m_rootWidget)
		{
			return m_inputCaptureWidget->onEvent(event);
		}
		return false;
	}
	
	bool UiCore::onMousePressEvent(EventMouse* event)
	{
		m_mouseCursor->setCurrentPos(event->getCurrentPosX(), event->getCurrentPosY());
		auto w = this->getTargetWidgetByPos(m_mouseCursor->getCurrentPos(), false);
		if (!w || m_inputCaptureWidget == m_rootWidget || w == m_rootWidget)
		{
			return false;
		}
		return w->onEvent(event);
	}
	
	bool UiCore::onMouseReleaseEvent(EventMouse* event)
	{
		m_mouseCursor->setCurrentPos(event->getCurrentPosX(), event->getCurrentPosY());
		auto w = this->getTargetWidgetByPos(m_mouseCursor->getCurrentPos(), false);
		if (w)
		{
			return w->onEvent(event);
		}
	
		return false;
	}
	
	bool UiCore::onMouseMoveEvent(EventMouse* event)
	{
		m_mouseCursor->setCurrentPos(event->getCurrentPosX(), event->getCurrentPosY());
	
		//DebugInfor("pos(%d,%d)",int( event->getCurrentPosX()), int(event->getCurrentPosY()));
	
		m_widgetContainingMouseIsUpTodate = false;
	
		if (!getWidgetContainingMouse(event))
		{
			return false;
		}
	
		//处理 当前在鼠标下面窗口的 鼠标移动事件 
		if (m_widgetContainingMouse && m_widgetContainingMouse != m_rootWidget)
		{
			return m_widgetContainingMouse->onEvent(event);
		}
	
		return false;
	}
	
	bool UiCore::onMouseDoubleClickedEvent(EventMouse* event)
	{
		m_mouseCursor->setCurrentPos(event->getCurrentPosX(), event->getCurrentPosY());
		auto w = this->getTargetWidgetByPos(m_mouseCursor->getCurrentPos(), false);
		if (w && w != m_rootWidget)
		{
			w->onEvent(event);
			return true;
		}
		return false;
	}
	
	bool UiCore::onCharacterEvent(EventCharacter* e)
	{
		auto w = this->getInputCaptureWidget();
		if (w)
		{
			return w->onEvent(e);
		}
	
		return false;
	}
	
	Widget* UiCore::getWidgetContainingMouse(EventMouse * event)
	{
		if (!m_widgetContainingMouseIsUpTodate)
		{
			updateWidgetContainingMouse(event);
			m_widgetContainingMouseIsUpTodate = true;
		}
	
		return m_widgetContainingMouse;
	}
	
	
	Camera* UiCore::getDefaultCamera() const
	{
		return m_uiCamera;
	}
	
	Widget * UiCore::getDefaultWidget()
	{
		return m_rootWidget;
	}
	
	
	void UiCore::setDefaultWidget(Widget* widget)
	{
		m_rootWidget = widget;
	
		m_inputCaptureWidget = nullptr;
	}
	
	Widget* UiCore::getInputCaptureWidget() const
	{
		return m_inputCaptureWidget;
	}
	
	void UiCore::setInputCaptureWidget(Widget* widget)
	{
		m_inputCaptureWidget = widget;
	}
	
	UiCore::UiCore()
		:m_rootWidget(nullptr)
		, m_mouseCursor(nullptr)
		, m_inputCaptureWidget(nullptr)
		, m_uiCamera(nullptr)
		, m_widgetContainingMouse(nullptr)
		, m_widgetContainingMouseIsUpTodate(true)
	{
		m_uiCamera = Camera::getDefultCamera();
		m_mouseCursor = std::make_shared<MouseCursor>();
	}
	
	bool UiCore::updateWidgetContainingMouse(EventMouse* event)
	{
		const Vector2& mousePos = m_mouseCursor->getCurrentPos();
		auto currentWidgetWithMouse = this->getTargetWidgetByPos(mousePos, true);
		if (currentWidgetWithMouse == m_widgetContainingMouse)
		{
			return false;
		}
	
		auto oldWidget = m_widgetContainingMouse;
		m_widgetContainingMouse = currentWidgetWithMouse;
	
		//对旧的窗口发送离开事件
		if (oldWidget)
		{
			WidgetEvent e(IEvent::Type::EventType_LeaveWIdget);
			oldWidget->onEvent(&e);
		}
	
		//对新的窗口发送鼠标进入事件
		if (m_widgetContainingMouse)
		{
			WidgetEvent e(IEvent::Type::EventType_EnterWidget);
			m_widgetContainingMouse->onEvent(&e);
		}
	
		return true;
	}
	
	
	Widget* UiCore::getTargetWidgetByPos(const Vector2 & pos, const bool allowDisabled)
	{
		if (!m_rootWidget || !m_rootWidget->isVisible())
		{
			return nullptr;
		}
	
		Widget* ret = nullptr;
	
	
		ret = m_rootWidget->getChildAtPos(pos, allowDisabled);
		if (!ret)
		{
			ret = m_rootWidget;
		}
	
	
		if (ret->isDistributeCapturedInputs())
		{
			auto w = ret->getChildAtPos(pos, allowDisabled);
			if (w)
			{
				ret = w;
			}
		}
	
		return ret;
	}
	
}
